package cn.z2huo.knowledge.basic.equalshashcode;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.Map;

/**
 * <p>
 *
 * @author z2huo
 */
class Test {

    @ToString
    @Accessors(chain = true)
    static class Obj {

        @Getter
        @Setter
        private int a;

        @Override
        public boolean equals(Object obj) {
            return a == ((Obj) obj).a;
        }

    }

    public static void main(String[] args) {
        Obj obj = new Obj().setA(10);
        Obj obj2 = new Obj().setA(20);
        Obj obj3 = new Obj().setA(20);
        Obj obj4 = obj3;

        System.out.println(obj.equals(obj2));
        System.out.println(obj.equals(obj3));
        System.out.println(obj2.equals(obj3));
        System.out.println(obj3.equals(obj4));
        System.out.println();

        System.out.println(obj.hashCode());
        System.out.println(obj2.hashCode());
        System.out.println(obj3.hashCode());
        System.out.println(obj4.hashCode());
        System.out.println();

        System.out.println(System.identityHashCode(obj));
        System.out.println();

        Map<Obj, String> map = Map.of(obj, "obj", obj2, "obj2", obj3, "obj3");
        System.out.println(map);

        System.out.println(map.get(obj));
        System.out.println(map.get(obj2));
        System.out.println(map.get(obj3));

    }

}
